package restaurant.utils;

import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BaseServlet
 */
public class BaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//接收参数
		String action = req.getParameter("action");
		try {
			//反射获取当前对象的字节码
			Class clazz = this.getClass();
			Method method = clazz.getMethod(action, HttpServletRequest.class,HttpServletResponse.class);
			//判断有没有传入的方法
			if (method != null) {
				//如果有调用
				String desPath = (String) method.invoke(this, req,resp);
				System.out.println(desPath);
				System.out.println(req.getContextPath());
				//转发
				if (desPath != null) {
					req.getRequestDispatcher(desPath).forward(req, resp);
				} 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}

}